#include "mainwindow.h"
#include "ui_mainwindow.h"

#include <CoreLib.h>
#include <PluginManager.h>
#include <QPluginLoader>
#include <UIlib.h>
#include <mybutton.h>
#include <qgenericplugin.h>
#include <sharea.h>


MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    //staticlib
    UIlib m_UIlib;
    m_UIlib.print();
    qDebug() << "=====";

    //插件
    // 加载插件
    QString pluginPath = findPlugin("pluginA/pluginA");
    qDebug() << "pluginPath:" << pluginPath;
    QPluginLoader loader(pluginPath);
    QObject *pluginInstance = loader.instance();
    if (!pluginInstance) {
        qDebug() << "Failed to load plugin:" << loader.errorString();
    }
    // 转换为 QGenericPlugin
    QGenericPlugin *plugin = qobject_cast<QGenericPlugin*>(pluginInstance);
    if (!plugin) {
        qDebug() << "Loaded plugin is not a QGenericPlugin";
    }

    // 使用插件创建对象
    QObject *toolManager = plugin->create("ToolManager", "");
    if (toolManager) {
        qDebug() << "Successfully created ToolManager";
        QMetaObject::invokeMethod(toolManager, "executeTool");
    }
    QObject *dialogManager = plugin->create("DialogManager", "");
    if (dialogManager) {
        qDebug() << "Successfully created DialogManager";
        QMetaObject::invokeMethod(dialogManager, "showDialog");
    }

    // 使用配置参数
    QObject *configTool = plugin->create("ConfigurableTool", "timeout=5000");
    if (configTool) {
        qDebug() << "Successfully created ConfigurableTool with configuration";
    }


    //pluginB 插件的使用
    PluginManager pluginManager;
    pluginManager.loadDesignerPlugins("pluginB");

    //MyButton * m = new MyButton( this );
   /// m->setText( "1111" );
    //ui->verticalLayout->addLayout( m );

    QWidget *MyButton = pluginManager.createWidget("MyButton");
    if (MyButton) {
        qDebug() << "hahaha  我成功了";
        //MyButton->setProperty("color", QColor(Qt::blue));
        //MyButton->show();
        //放开注释可看效果
    }
    ///share 库的使用
    ShareA * m_ShareA = new ShareA();
    m_ShareA->print();

    delete m_ShareA;

    //导出的调用 无需h头
    void *m_ShareA2  = createShareAInstance();
    // ShareA* buttonWidget = static_cast<ShareA*>(m_ShareA2);
    // buttonWidget->print();

    printMessage( m_ShareA2 );

    // destroyShareAInstance(m_ShareA2);
    // delete buttonWidget;

    CoreLib * m_CoreLib =  new CoreLib();
    m_CoreLib->print();
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::on_pushButton_clicked()
{

}

